package cn.com.note.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.note.R;

/**
 * Created by SunFuLin on 2019/1/8.
 */

public class MenuPop extends PopupWindow {
    ListView listView;
    List<String> data;
    Context context;
    MenuAdapter adapter;
    int mPosition = -1;
    public Map<Integer, Boolean> enableList = new HashMap<>();

    public interface CallBack {
        void click(int position, String content);
    }

    private CallBack callBack;

    public MenuPop(Context c, List<String> list) {
        super(c);
        this.context = c;
        data = list;

        initEnableList();
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setOutsideTouchable(false);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.parseColor("#5f000000")));
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_menu,
                null, false);
        listView = (ListView) contentView.findViewById(R.id.list_menu);
        adapter = new MenuAdapter();
        listView.setAdapter(adapter);
        setContentView(contentView);
    }

    /**
     * 初始化所有按钮为可用状态
     */
    private void initEnableList() {
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                enableList.put(i, true);
            }
        }
    }

    /**
     * 获取当前按钮的 enable 状态
     *
     * @param tag
     * @return
     */
    public boolean getIsEnable(int tag) {
        return enableList.get(tag);
    }

    /**
     * 设置点击监听回调
     *
     * @param callBack
     */
    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    /**
     * 刷新列表
     */
    public void notifyDataSetChanged() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 更新 当前TAG 的按钮可用状态
     *
     * @param tag
     * @param isEnable
     */
    public void updateUIEnable(int tag, boolean isEnable) {
        enableList.put(tag, isEnable);
        notifyDataSetChanged();
    }

    /**
     * 隐藏pop
     */
    public void dismissPop() {
        this.dismiss();
    }

    /**
     * 内部类适配器
     */
    class MenuAdapter extends BaseAdapter {
        public MenuAdapter() {
        }

        @Override
        public int getCount() {
            if (data != null && data.size() > 0) {
                return data.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_menu, parent, false);
                holder = new ViewHolder();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.button = convertView.findViewById(R.id.button);
            holder.iv = convertView.findViewById(R.id.iv);
            holder.ll = convertView.findViewById(R.id.ll);
            holder.button.setText(data.get(position));
            if (mPosition == position) {
                holder.button.setTextColor(ContextCompat.getColor(context, R.color.colorClicked));
                holder.iv.setVisibility(View.VISIBLE);
            } else {
                holder.button.setTextColor(ContextCompat.getColor(context, R.color.colorClickedDefault));
                holder.iv.setVisibility(View.GONE);
            }
            if (!enableList.get(position)) {
                holder.button.setTextColor(ContextCompat.getColor(context, R.color.colorUnEnabled));
                holder.iv.setVisibility(View.GONE);
                holder.button.setEnabled(false);
            } else {
                holder.button.setEnabled(true);
            }
            holder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //处理打勾
                    //将点击的位置传出去
                    mPosition = position;
                    notifyDataSetChanged();
                    callBack.click(mPosition, data.get(position));
                }
            });
            return convertView;
        }

        class ViewHolder {
            TextView button;
            RelativeLayout ll;
            ImageView iv;
        }
    }
}
